import Vue from "vue";
import Vuex from "vuex";
import configsprocess from "@/services/configsapi";

Vue.use(Vuex);

const fetchBar = function() {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      console.log(111111);
      resolve("bar 组件返回 ajax 数据");
    }, 1000);
    console.log(reject);
  });
};

function createStore() {
  const store = new Vuex.Store({
    state: {
      bar: "",
      companysysno: 1717,
      webconfigs: []
    },

    mutations: {
      SET_BAR(state, data) {
        state.bar = data;
      },
      set_configdatas(state, data) {
        Vue.set(state, "webconfigs", data);
        //state.webconfigs = data;
      },
      set_CompanySysNo(state, sysno) {
        state.companysysno = sysno;
      }
    },

    actions: {
      fetchBar({ commit }) {
        return fetchBar()
          .then(data => {
            commit("SET_BAR", data);
          })
          .catch(err => {
            console.error(err);
          });
      },
      async fetchConfigDatas({ commit }, data) {
        await configsprocess.Init(data);
        commit("set_configdatas", configsprocess.configs);
      },
      setCompanySysNo({ commit }, data) {
        commit("set_CompanySysNo", data);
      }
    }
  });

  if (typeof window !== "undefined" && window.__INITIAL_STATE__) {
    console.log("window.__INITIAL_STATE__", window.__INITIAL_STATE__);
    store.replaceState(window.__INITIAL_STATE__);
  } else {
    console.log("no browser");
  }

  return store;
}

export default createStore;
